/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Medicine;

import Bill.Bill;
import Custommer.Custommer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import main.ketnoi;

/**
 *
 * @author ADMIN
 */
public class MedicineForBill extends javax.swing.JFrame {

    Connection con;
    DefaultTableModel model;
    DefaultTableModel model2;

    /**
     * Creates new form MedicineForBill
     */
    public MedicineForBill() {
        try {
            initComponents();
            ketnoi a = new ketnoi();
            con = a.connect();
            model = (DefaultTableModel) jTable1.getModel();
            model2 = (DefaultTableModel) jTable2.getModel();
            PreparedStatement prst1 = con.prepareStatement("Select * from MedicineTypes");
            ResultSet rs1 = prst1.executeQuery();
            while (rs1.next()) {
                Vector v1 = new Vector();
                v1.add(rs1.getString(1));
                v1.add(rs1.getString(2));
                model2.addRow(v1);
            }
            loadData();
            //code auto for CustomerCode
            jTextField1.setEnabled(false);
            int num1 = jTable1.getRowCount();
            jTextField1.setText("M" + (num1 + 1));
            //code auto for CustomerType
            jTextField4.setEnabled(false);
            int num2 = jTable2.getRowCount();
            jTextField4.setText("MT" + (num2 + 1));
            cbsearch.addItem("Thuoc gan het");
        } catch (SQLException ex) {
            Logger.getLogger(MedicineForBill.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void loadData() {
        try {

            PreparedStatement prst = con.prepareStatement("Select * from Medicines");
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                Vector v = new Vector();
                v.add(rs.getString(1));
                v.add(rs.getString(2));
                v.add(rs.getString(3));
                v.add(rs.getString(4));
                v.add(rs.getString(5));
                v.add(rs.getString(6));
                model.addRow(v);
            }
            jComboBox1.addItem("---Select---");
            PreparedStatement pre = con.prepareStatement("Select distinct MedicineTypeName from MedicineTypes");
            ResultSet re = pre.executeQuery();
            while (re.next()) {
                jComboBox1.addItem(re.getString(1));
            }

            jComboBox2.addItem("---Select---");
            PreparedStatement prep = con.prepareStatement("Select distinct SupplierName from Suppliers");
            ResultSet res = prep.executeQuery();
            while (res.next()) {
                jComboBox2.addItem(res.getString(1));
            }
            cbsearch.addItem("---Select---");
            PreparedStatement st = con.prepareStatement("Select top 4 * from information_schema.columns where table_name = 'medicines'");
            ResultSet rs2 = st.executeQuery();
            while (rs2.next()) {
                //lay du lieu cot thu 4: name,diem,lop
                String str1 = rs2.getString(4);
                cbsearch.addItem(str1);

            }

        } catch (SQLException ex) {
            Logger.getLogger(Custommer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static boolean checkNumber(String n) {
        if (n == null || n.length() > 10 || n.length() < 1) {
            return false;
        } else {
            String strPattern = "[^0-9]";
            Pattern p;
            Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern, flag);
            m = p.matcher(n);
            return !m.find();
        }
    }

    public void reset() {
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField6.setText("");
        jComboBox1.setSelectedIndex(0);
        jComboBox2.setSelectedIndex(0);
        //code auto for CustomerCode
        jTextField1.setEnabled(false);
        int num1 = jTable1.getRowCount();
        jTextField1.setText("M" + (num1 + 1));

    }

    public void reset2() {
        jTextField4.setText("");
        jTextField5.setText("");
        //code auto for CustomerType
        jTextField4.setEnabled(false);
        int num2 = jTable2.getRowCount();
        jTextField4.setText("MT" + (num2 + 1));

    }

    public boolean checkMedicineCode() {
        try {
            if (jTextField1.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "MedicineCode invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
                jTextField1.setText("");
                jTextField1.requestFocus();
                return false;
            }
            PreparedStatement pre = con.prepareStatement("select MedicineCode from Medicines");
            ResultSet re = pre.executeQuery();
            while (re.next()) {
                if (jTextField1.getText().equals(re.getString(1))) {
                    JOptionPane.showMessageDialog(this, "MedicineCode existed!");
                    //code auto for MedicineCode
                    jTextField1.setEnabled(false);
                    int num1 = jTable1.getRowCount();
                    Calendar now = Calendar.getInstance();
                    jTextField1.setText("M" + (num1 + 1));
                    return false;
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(Medicine.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    public boolean checkMedicine() {
        if (jTextField2.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Name invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            jTextField2.setText("");
            jTextField2.requestFocus();
            return false;
        } else if (jTextField3.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Amount invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            jTextField3.setText("");
            jTextField3.requestFocus();
            return false;
        } else if (checkNumber(jTextField3.getText()) == false) {
            JOptionPane.showMessageDialog(this, "Amount Invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (jTextField6.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Price Invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (checkNumber(jTextField6.getText()) == false) {
            JOptionPane.showMessageDialog(this, "Price Invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (jComboBox1.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Type Invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (jComboBox2.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Supplier Invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        return true;
    }

    public boolean checkMedicineTypeCode() {
        try {
            if (jTextField4.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "MedicineTypeCode invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
                jTextField4.setText("");
                jTextField4.requestFocus();
                return false;
            }
            PreparedStatement pre = con.prepareStatement("select MedicineTypeCode from MedicineTypes");
            ResultSet re = pre.executeQuery();
            while (re.next()) {
                if (jTextField4.getText().equals(re.getString(1))) {
                    JOptionPane.showMessageDialog(this, "MedicineTypeCode existed!", "ERROR", JOptionPane.ERROR_MESSAGE);
                    //code auto for CustomerCode
                    jTextField4.setEnabled(false);
                    int num1 = jTable1.getRowCount();
                    Calendar now = Calendar.getInstance();
                    jTextField4.setText("MT" + (num1 + 1));
                    return false;
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(Medicine.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    public boolean checkMedicineType() {
        if (jTextField5.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Name invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
            jTextField5.setText("");
            jTextField5.requestFocus();
            return false;
        }

        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        cbsearch = new javax.swing.JComboBox();
        txtsearch = new javax.swing.JTextField();
        search = new javax.swing.JButton();
        sort = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        update = new javax.swing.JButton();
        reset = new javax.swing.JButton();
        add = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel7 = new javax.swing.JPanel();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButtonAdd = new javax.swing.JButton();
        jButtonReset = new javax.swing.JButton();
        jButtonDelete = new javax.swing.JButton();
        jButton5update = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(231, 231, 231));
        jPanel1.setPreferredSize(new java.awt.Dimension(777, 702));

        jTabbedPane1.setBackground(new java.awt.Color(231, 231, 231));
        jTabbedPane1.setPreferredSize(new java.awt.Dimension(777, 702));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "MedicineList", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Code", "Name", "Supplier Code", "Medicine Type Code", "Amount", "Price"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        search.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/search2.png"))); // NOI18N
        search.setText("Search");
        search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchActionPerformed(evt);
            }
        });

        sort.setText("Sort");
        sort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(sort, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(cbsearch, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addComponent(txtsearch, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sort, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(cbsearch))
                    .addComponent(search, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtsearch, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Detail", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));

        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jLabel1.setText("MedicineCode");

        jLabel2.setText("MedicineName");

        jLabel3.setText("MedicineAmount");

        jLabel4.setText("MedicineTypeName");

        jLabel5.setText("Supplier");

        jLabel8.setText("Price");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .addComponent(jTextField2)
                    .addComponent(jTextField3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(55, 55, 55))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTextField6)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        update.setText("Update");
        update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateActionPerformed(evt);
            }
        });

        reset.setText("New");
        reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetActionPerformed(evt);
            }
        });

        add.setText("Add");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(54, 54, 54)
                        .addComponent(update, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(62, 62, 62)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(add)
                    .addComponent(reset)
                    .addComponent(update)
                    .addComponent(jButton1))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Medicines", jPanel3);

        jPanel5.setBackground(new java.awt.Color(231, 231, 231));

        jPanel6.setBackground(new java.awt.Color(231, 231, 231));
        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("MedicineTypeList"));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "MedicineTypeCode", "MedicineTypeName"
            }
        ));
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(58, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        jPanel7.setBackground(new java.awt.Color(231, 231, 231));
        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Detail"));

        jLabel6.setText("MedicineTypeCode");

        jLabel7.setText("MedicineTypeName");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(131, 131, 131)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(46, 46, 46)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(138, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        jButtonAdd.setText("Add");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jButtonReset.setText("New");
        jButtonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonResetActionPerformed(evt);
            }
        });

        jButtonDelete.setText("Delete");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jButton5update.setText("Update");
        jButton5update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5updateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(jButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(jButtonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42)
                        .addComponent(jButton5update, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34)
                        .addComponent(jButtonReset, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAdd)
                    .addComponent(jButtonDelete)
                    .addComponent(jButton5update)
                    .addComponent(jButtonReset))
                .addContainerGap(59, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("MedicineType", jPanel5);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 491, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(200, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 777, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 702, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        try {
            // TODO add your handling code here:
            int r = jTable1.getSelectedRow();
            jTextField1.setText(jTable1.getValueAt(r, 0).toString());
            jTextField2.setText(jTable1.getValueAt(r, 1).toString());
            jTextField3.setText(jTable1.getValueAt(r, 4).toString());
            jTextField6.setText(jTable1.getValueAt(r, 5).toString());
            //Combobox cho Supplier
            //            System.out.println(jTable1.getValueAt(r, 2));
            PreparedStatement prst = con.prepareStatement("select SupplierName from Suppliers where SupplierCode = ?");
            prst.setString(1, jTable1.getValueAt(r, 2).toString());
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                jComboBox2.setSelectedItem(rs.getString(1));
            }

            //Combobox cho MedicineType
            //           System.out.println(jTable1.getValueAt(r, 3).toString());
            PreparedStatement prst1 = con.prepareStatement("select MedicineTypeName from MedicineTypes where MedicineTypeCode = ?");
            prst1.setString(1, jTable1.getValueAt(r, 3).toString());
            ResultSet rs1 = prst1.executeQuery();
            while (rs1.next()) {
                jComboBox1.setSelectedItem(rs1.getString(1));

            }

            //        if (jTable1.getValueAt(r, 3).equals("MT1")) {
            //            jComboBox1.setSelectedItem("Vitamin");
            //        } else if (jTable1.getValueAt(r, 3).equals("MT2")) {
            //            jComboBox1.setSelectedItem("Drug");
            //        } else jComboBox1.setSelectedItem("Poison");
        } catch (SQLException ex) {
            Logger.getLogger(Medicine.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed
        if (cbsearch.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Please select search condition!");
        } else {

            if (cbsearch.getSelectedItem().equals("Thuoc gan het")) {
                try {
                    this.reset();
                    int num = jTable1.getRowCount();
                    for (int i = num - 1; i >= 0; i--) {
                        model.removeRow(i);
                    }

                    PreparedStatement pre = con.prepareStatement("select * from Medicines where MedicineAmount < 250");
                    pre.executeQuery();
                    ResultSet re = pre.executeQuery();
                    Vector e = null;
                    while (re.next()) {
                        e = new Vector();
                        e.add(re.getString(1));
                        e.add(re.getString(2));
                        e.add(re.getString(3));
                        e.add(re.getString(4));
                        e.add(re.getString(5));
                        e.add(re.getString(6));
                        model.addRow(e);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(Medicine.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {

                try {
                    // TODO add your handling code here:
                    this.reset();
                    int num = jTable1.getRowCount();
                    for (int i = num - 1; i >= 0; i--) {
                        model.removeRow(i);
                    }
                    String str1 = cbsearch.getSelectedItem().toString();
                    String str = txtsearch.getText();
                    PreparedStatement pst = con.prepareStatement("Select * from medicines where " + str1 + " = ?");
                    pst.setString(1, str);
                    ResultSet rs1 = pst.executeQuery();
                    Vector e = null;
                    while (rs1.next()) {
                        e = new Vector();
                        e.add(rs1.getString(1));
                        e.add(rs1.getString(2));
                        e.add(rs1.getString(3));
                        e.add(rs1.getString(4));
                        e.add(rs1.getString(5));
                        e.add(rs1.getString(6));
                        model.addRow(e);
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(Medicine.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_searchActionPerformed

    private void sortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sortActionPerformed
        if (cbsearch.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Please select sort condition!");
        } else {
            try {
                // TODO add your handling code here:
                this.reset();
                int num = jTable1.getRowCount();
                for (int i = num - 1; i >= 0; i--) {
                    model.removeRow(i);
                }
                String str1 = cbsearch.getSelectedItem().toString();
                PreparedStatement pst = con.prepareStatement("Select * from medicines order by " + str1);
                ResultSet rs1 = pst.executeQuery();
                Vector e = null;
                while (rs1.next()) {
                    e = new Vector();
                    e.add(rs1.getString(1));
                    e.add(rs1.getString(2));
                    e.add(rs1.getString(3));
                    e.add(rs1.getString(4));
                    e.add(rs1.getString(5));
                    e.add(rs1.getString(6));
                    model.addRow(e);
                }
            } catch (SQLException ex) {
                Logger.getLogger(Medicine.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_sortActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateActionPerformed
        if (jTextField1.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please select Medicine from table above");
        } else {
            try {
                // TODO add your handling code here:
                //Lay gia tri cua combobox MedicineTypeName luu vao bien a
                PreparedStatement prst = con.prepareStatement("select * from MedicineTypes where MedicineTypeName=?");
                prst.setString(1, jComboBox1.getSelectedItem().toString());
                ResultSet rs = prst.executeQuery();
                String a = null;
                while (rs.next()) {
                    a = rs.getString(1);
                }

                //Lay gia tri cua combobox Supplier luu vao bien b
                PreparedStatement prst1 = con.prepareStatement("select * from Suppliers where SupplierName=?");
                prst1.setString(1, jComboBox2.getSelectedItem().toString());
                ResultSet rs1 = prst1.executeQuery();
                String b = null;
                while (rs1.next()) {
                    b = rs1.getString(1);
                }

                //Update
                int c = Integer.parseInt(jTextField6.getText());
                PreparedStatement prst2 = con.prepareStatement("UPDATE Medicines SET MedicineName=?,SupplierCode=?,MedicineTypeCode=?,MedicineAmount=?,Price=? WHERE MedicineCode=?");
                prst2.setString(6, jTextField1.getText());
                prst2.setString(1, jTextField2.getText());
                prst2.setString(4, jTextField3.getText());
                prst2.setInt(5, c);
                prst2.setString(3, a);
                prst2.setString(2, b);
                prst2.executeUpdate();
                //Load lai CSDL
                int num = jTable1.getRowCount();
                for (int i = num - 1; i >= 0; i--) {
                    model.removeRow(i);
                }
                PreparedStatement prst3 = con.prepareStatement("Select * from Medicines");
                ResultSet rs2 = prst3.executeQuery();
                while (rs2.next()) {
                    Vector b1 = new Vector();
                    b1.add(rs2.getString(1));
                    b1.add(rs2.getString(2));
                    b1.add(rs2.getString(3));
                    b1.add(rs2.getString(4));
                    b1.add(rs2.getString(5));
                    b1.add(rs2.getString(6));
                    model.addRow(b1);
                }
                this.reset();
            } catch (SQLException ex) {
                Logger.getLogger(Medicine.class
                        .getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Chua nhap du thong tin!\nThong tin nhap chua chinh xac!", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_updateActionPerformed

    private void resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetActionPerformed
        // TODO add your handling code here:
        this.reset();
    }//GEN-LAST:event_resetActionPerformed

    private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
        if (checkMedicineCode() == true) {
            if (checkMedicine() == true) {
                try {
                    //test
                    //                    System.out.println(cbsearch.getSelectedItem());
                    //                    System.out.println(cbsearch.getSelectedObjects());
                    // TODO add your handling code here:

                    //Lay gia tri cua combobox MedicineTypeName luu vao bien a
                    PreparedStatement prst = con.prepareStatement("select * from MedicineTypes where MedicineTypeName=?");
                    prst.setString(1, jComboBox1.getSelectedItem().toString());
                    ResultSet rs = prst.executeQuery();
                    String a = null;
                    while (rs.next()) {
                        a = rs.getString(1);
                    }

                    //Lay gia tri cua combobox Supplier luu vao bien b
                    PreparedStatement prst1 = con.prepareStatement("select * from Suppliers where SupplierName=?");
                    prst1.setString(1, jComboBox2.getSelectedItem().toString());
                    ResultSet rs1 = prst1.executeQuery();
                    String b = null;
                    while (rs1.next()) {
                        b = rs1.getString(1);
                    }

                    //Insert
                    PreparedStatement prst2 = con.prepareStatement("insert into Medicines values (?,?,?,?,?,?)");
                    prst2.setString(1, jTextField1.getText());
                    prst2.setString(2, jTextField2.getText());
                    prst2.setString(5, jTextField3.getText());
                    prst2.setString(6, jTextField6.getText());
                    prst2.setString(4, a);
                    prst2.setString(3, b);
                    prst2.executeUpdate();

                    // Load lai co so du lieu
                    int num = jTable1.getRowCount();
                    for (int i = num - 1; i >= 0; i--) {
                        model.removeRow(i);
                    }
                    PreparedStatement prst3 = con.prepareStatement("Select * from Medicines");
                    ResultSet rs3 = prst3.executeQuery();
                    while (rs3.next()) {
                        Vector b1 = new Vector();
                        b1.add(rs3.getString(1));
                        b1.add(rs3.getString(2));
                        b1.add(rs3.getString(3));
                        b1.add(rs3.getString(4));
                        b1.add(rs3.getString(5));
                        b1.add(rs3.getString(6));
                        model.addRow(b1);
                    }
                    this.reset();
                } catch (SQLException ex) {
                    Logger.getLogger(Medicine.class
                            .getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Chua nhap du thong tin!\nThong tin nhap chua chinh xac!", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_addActionPerformed

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
        // TODO add your handling code here:
        int r = jTable2.getSelectedRow();
        if (r < 0) {
            return;
        }
        jTextField4.setText(jTable2.getValueAt(r, 0).toString());
        jTextField5.setText(jTable2.getValueAt(r, 1).toString());
    }//GEN-LAST:event_jTable2MouseClicked

    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        if (checkMedicineTypeCode() == true) {
            if (checkMedicineType() == true) {
                try {
                    // TODO add your handling code here:
                    PreparedStatement prst = con.prepareStatement("insert into MedicineTypes values (?,?)");
                    prst.setString(1, jTextField4.getText());
                    prst.setString(2, jTextField5.getText());
                    prst.executeUpdate();

                    // Load lai co so du lieu
                    int num = jTable2.getRowCount();
                    for (int i = num - 1; i >= 0; i--) {
                        model2.removeRow(i);
                    }
                    PreparedStatement prst1 = con.prepareStatement("Select * from MedicineTypes");
                    ResultSet rs1 = prst1.executeQuery();
                    while (rs1.next()) {
                        Vector b1 = new Vector();
                        b1.add(rs1.getString(1));
                        b1.add(rs1.getString(2));
                        model2.addRow(b1);

                        //Load lai combobox
                        jComboBox1.removeAllItems();
                        jComboBox1.addItem("---Select---");
                        PreparedStatement pre = con.prepareStatement("Select distinct MedicineTypeName from MedicineTypes");
                        ResultSet re = pre.executeQuery();
                        while (re.next()) {
                            jComboBox1.addItem(re.getString(1));
                        }
                    }
                    this.reset2();
                } catch (SQLException ex) {
                    Logger.getLogger(Medicine.class
                            .getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Chua nhap du thong tin!\nThong tin nhap chua chinh xac!", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_jButtonAddActionPerformed

    private void jButtonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResetActionPerformed
        // TODO add your handling code here:
        this.reset2();
    }//GEN-LAST:event_jButtonResetActionPerformed

    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        int n = JOptionPane.showConfirmDialog(this, "     Are you sure? ", "---DELETE---", JOptionPane.YES_NO_OPTION, 2);
        if (n == 0) {
            try {
                // TODO add your handling code here:
                String str = null;
                PreparedStatement prst2 = con.prepareStatement("Delete from MedicineTypes where MedicineTypeCode = ?");
                str = jTable2.getValueAt(jTable2.getSelectedRow(), 0).toString();
                prst2.setString(1, str);
                prst2.executeUpdate();
                model2.removeRow(jTable2.getSelectedRow());
                this.reset2();

                //Load lai combobox
                jComboBox1.removeAllItems();
                jComboBox1.addItem("---Select---");
                PreparedStatement pre = con.prepareStatement("Select distinct MedicineTypeName from MedicineTypes");
                ResultSet re = pre.executeQuery();
                while (re.next()) {
                    jComboBox1.addItem(re.getString(1));
                }

            } catch (SQLException ex) {
                Logger.getLogger(Medicine.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButtonDeleteActionPerformed

    private void jButton5updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5updateActionPerformed
        try {
            // TODO add your handling code here:
            PreparedStatement prst = con.prepareStatement("UPDATE MedicineTypes SET MedicineTypeName=? WHERE MedicineTypeCode=?");
            prst.setString(2, jTextField4.getText());
            prst.setString(1, jTextField5.getText());
            prst.executeUpdate();
            //Load lai CSDL
            this.reset2();
            int num = jTable2.getRowCount();
            for (int i = num - 1; i >= 0; i--) {
                model2.removeRow(i);
            }
            PreparedStatement prst1 = con.prepareStatement("Select * from MedicineTypes");
            ResultSet rs1 = prst1.executeQuery();
            while (rs1.next()) {
                Vector b1 = new Vector();
                b1.add(rs1.getString(1));
                b1.add(rs1.getString(2));
                model2.addRow(b1);
            }
            this.reset2();

            //Load lai combobox
            jComboBox1.removeAllItems();
            jComboBox1.addItem("---Select---");
            PreparedStatement pre = con.prepareStatement("Select distinct MedicineTypeName from MedicineTypes");
            ResultSet re = pre.executeQuery();
            while (re.next()) {
                jComboBox1.addItem(re.getString(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Medicine.class
                    .getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Chua nhap du thong tin!\nThong tin nhap chua chinh xac!", "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton5updateActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Bill b = new Bill();
        b.test(jTextField1.getText());
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MedicineForBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MedicineForBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MedicineForBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MedicineForBill.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MedicineForBill().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton add;
    private javax.swing.JComboBox cbsearch;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton5update;
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonReset;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JButton reset;
    private javax.swing.JButton search;
    private javax.swing.JButton sort;
    private javax.swing.JTextField txtsearch;
    private javax.swing.JButton update;
    // End of variables declaration//GEN-END:variables
}
